<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort() + request.getContextPath();
%>
<jsp:include page="/WEB-INF/page/auth/_header.jsp"></jsp:include>
<body>
	<jsp:include page="/WEB-INF/page/auth/header.jsp"></jsp:include>
	<div id="middle">
		<jsp:include page="/WEB-INF/page/auth/menu.jsp"></jsp:include>
		<div id="right">
			<div id="kccs_course_specialtb">
		        <form id="kccs_course_specialForm" class="m10">
		           	<span>${term.termName}</span>
		           	特长班：<select class="form-control1" name="courseId" id="courseId" style="width: 150px;"></select>
		            <select class="form-control1" name="grade" id="grade" onchange="searchClazz('kccs_course_special',this.value)"></select>
		            <select class="form-control1" name="clazz" id="clazz"></select>
		                             学生姓名：
		            <input class="form-control1" type="text" name="studentName" id="studentName" style="width: 120px;">
					<input type="hidden" id="schoolId" name="schoolId" value=""/>
		            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-search'" onclick="searchForm('kccs_course_special')">查询</a>
		            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-clear'" onclick="resetForm('kccs_course_special')">重置</a>
		        </form>
		    </div>
		    <table id="kccs_course_specialdg">
		        <thead>
		            <tr>
		                <th data-options="field:'id',checkbox:true"></th>
		                <th data-options="field:'gradeName',title:'年级'"></th>
		                <th data-options="field:'clazz',title:'班号'"></th>
		                <th data-options="field:'studentNum',title:'学号'"></th>
		                <th data-options="field:'studentId',title:'学生姓名',editor:{
		                    type:'combobox',
		                    options:{
		                        valueField:'id',
		                        textField:'name',
		                        method:'POST',
		                        url: '<%=basePath %>/studentList',
								require: true
		                    }
		                },formatter: function(value, row, index) {
		                	return row.studentName;
		                }"></th>
		                <th data-options="field:'studentIdNoType',title:'证件类型'"></th>
		                <th data-options="field:'studentIdNo',title:'证件号'"></th>
		                <th data-options="field:'studentSex',title:'性别'"></th>
		                <th data-options="field:'courseId',title:'课程名称',editor:{
		                    type:'combobox',
		                    options:{
		                        valueField:'id',
		                        textField:'courseName',
		                        method:'POST',
		                        url: '<%=basePath %>/sys/kccsCourse/special/courseList',
								require: true
		                    }
		                },formatter: function(value, row, index) {
		                	return row.courseName;
		                }"></th>
		                <th data-options="field:'classTeacher',title:'上课老师'"></th>
		                <th data-options="field:'classDate',title:'上课时间'"></th>
		                <th data-options="field:'classAddr',title:'上课地点'"></th>
		            </tr>
		        </thead>
		    </table>
		</div>
	</div>
	
	<div id="uploadBox" style="display: none;height: 'auto';">
	    <div class="col-md-12 pd20">
   			<form id="uploadForm" novalidate="novalidate" enctype="multipart/form-data" method="post">
				<div class="form-group col-md-12" id="scoreTips">
					
				</div>
				<div class="form-group col-md-12 mt20">
					<input class="easyui-filebox form-control" id="files" name="files" style="width:250px;">
				</div>
			</form>
		</div>
	    <div class="text-center pd20">
	        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="uploadCommit()" style="width: 200px;">上传</a>
	    </div>
	</div>
	
	<jsp:include page="/WEB-INF/page/auth/_footer.jsp"/>
	<script type="text/javascript">
		var url = basePath + "/sys/kccsCourse/special";
		var schoolId = $("#userSchoolId").val();
		$(function(){
			initSchoolIdValue();
			loadDatagrid('kccs_course_special',url);
			initGrade("kccs_course_special");
            courseList("kccs_course_special");
            $('#files').filebox({
                buttonText: '选择文件',
                buttonAlign: 'left'
            })
		})
		function loadDatagrid(cf, url){
        	dgEditIndex[cf]=-1;
        	var btnArr = [];
        	btnArr.push({text:'新增',iconCls:'icon-add',handler:function(){add(cf)}},'-');
        	btnArr.push({text:'删除',iconCls:'icon-remove',handler:function(){del(cf, url)}},'-')
        	btnArr.push({text:'保存',iconCls:'icon-save',handler:function(){save(cf)}},'-');
       		btnArr.push({text:'取消',iconCls:'icon-undo',handler:function(){undo(cf)}},'-');
       		btnArr.push({text:'下载excel模板',iconCls:'icon-tip',handler:function(){downloadTemplate('特长班名单模板.xls')}},'-');
       		btnArr.push({text:'导出名单',iconCls:'icon-save',handler:function(){exportExcel('kccs_course_special','<%=basePath%>/sys/kccsCourse/special/export')}},'-');
       		btnArr.push({text:'导入',iconCls:'icon-large-smartart',handler:function(){upload()}},'-');
        	$("#"+cf+"dg").datagrid({
        		url: url + "/pageQuery",
        		rownumbers: true,
        		striped: true,
        		border: false,
        		pagination: true,
        		pageSize: 30,
        		fitColumns: true,
        		autoRowHeight: false,
        		singleSelect: false,
				queryParams : {
					schoolId: $("#userSchoolId").val()
				},
        		fit: true,
        		toolbar: btnArr,
        	    onAfterEdit: function(rowIndex,rowData,changes){
        	    	if($.isEmptyObject(changes)) {
        	    		var dgObj=$('#'+cf+'dg');
        				dgObj.datagrid('rejectChanges');
        				dgEditIndex[cf]=-1;
        				return;	
        			}
        			changes.id = rowData.id;
        	    	changes.schoolId = schoolId;
        	    	addmodify(changes, cf, url)
        	    },
        		onLoadSuccess: function(data){
        	        $(".note").tooltip({
        		        onShow: function () {
        			        $(this).tooltip('tip').css({
        			       		borderColor: '#000'
        			        });
        		        }
        	        });
        		}
        	})
        	var tb = $("#"+cf+"tb").html();
        	if(tb) {
        		$(".datagrid-toolbar").prepend(tb);
        		$("#"+cf+"tb").remove();
        	}
        }
		function courseList(cf) {
			$.ajax({
				url: url + "/courseList",
				data: {schoolId:schoolId},
				type: 'GET'
			}).done(function(data) {
				var html = "<option value=''>请选择</option>";
				for(var i in data) {
					html += "<option value='"+data[i].id+"'>"+data[i].courseName+"</option>"
				}
				$("#" + cf + "Form #courseId").append(html);
			}).fail(function() {
				$('#msg').html('error,发生错误请及时联系管理员!');
			});
		}
		var uploadBox;
		function upload() {
			var courseId = $("#courseId").val();
			if(!courseId) {
				$.messager.alert('提醒','请选择特长班!');
				return;
			}
			var courseName = $("#courseId option:selected")[0].text;
			$("#scoreTips").html("<span class='calendar-sunday'>确认选择上传(" + courseName + ")的学员名单，以免名单上传出错</span>");
			uploadBox = $('#uploadBox').dialog({
				title : '上传学员名单',
				modal : true,
				width : 400,
				top : 200,
				draggable : true,
				resizable : true,
				buttons : '#btns',
				onClose : function() {
					$('#files').filebox('setValue','');
				}
			}).show();
		}

		function uploadCommit(){
			var fileObj = document.getElementById('filebox_file_id_2').files[0];
			if(!fileObj) {
				$.messager.alert('提醒','请选择文件');
				return;
			}
			$.messager.progress({
				title : '提示',
				msg : '上传中，请稍候...'
			});
			var xhr = new XMLHttpRequest();
			xhr.open('POST', url + "/import");
			xhr.onload = function () {
		       if (xhr.status === 200) {
		    	    $.messager.progress('close');
			       	var resp = JSON.parse(xhr.response);
			       	if(resp.code == '000') {
			       		$.messager.alert("成功","上传成功！","info");
			       		reload("kccs_course_special");
			       		$('#files').filebox('setValue','');
			       		uploadBox.dialog('close');
			       	}else {
			       		$.messager.alert('错误提醒', resp.msg);
			       	}
		       }
		   };
		   xhr.onerror = function () {
			   $.messager.progress('close');
			   $.messager.alert('错误', "上传异常");
		   };
		   var fd = new FormData();
		   fd.append('files', fileObj);
		   fd.append("courseId", $("#courseId").val());
			fd.append("schoolId",schoolId);
		   // 触发上传
			setTimeout(function() {
				xhr.send(fd);
			}, 1000)
		}
    </script>
</body>
</html>